<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="岗位编码">
        <el-input v-model="formInline.postCode" placeholder="岗位编码"></el-input>
      </el-form-item>
      <el-form-item label="岗位名称">
        <el-input v-model="formInline.postName" placeholder="岗位名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.status" placeholder="状态">
          <el-option label="停用" value="1"></el-option>
          <el-option label="正常" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button>
        <el-button type="primary" @click="chong">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" plain icon="el-icon-edit"  @click="openadd">新增</el-button>
    <el-button type="danger" icon="el-icon-delete" plain @click="pishan">批删</el-button>
    <el-button type="warning" icon="el-icon-star-off" plain @click="excel">导出</el-button>
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          prop="postId"
          label="部门id"
          width="180">
      </el-table-column>
      <el-table-column
          prop="postCode"
          label="岗位编码"
          width="180">
      </el-table-column>
      <el-table-column
          prop="postName"
          label="岗位名称">
      </el-table-column>



      <el-table-column
          prop="postSort"
          label="显示顺序">
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态(0正常 1停用)">
        <template v-slot="s">
          <el-switch
              v-model="s.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0">
          </el-switch>
<!--          <span v-if="s.row.status==0">停用</span>-->
<!--          <span v-if="s.row.status==1">正常</span>-->
        </template>
      </el-table-column>
      <el-table-column
          prop="createBy"
          label="创建者">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间">
      </el-table-column>
      <el-table-column
          prop="updateBy"
          label="更新者">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="更新时间">
      </el-table-column>
      <el-table-column
          prop="remark"
          label="备注">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button type="success" plain  icon="el-icon-check" @click="openEdit(scope.row)"  size="small">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" plain @click="del(scope.row.postId)"  size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @current-change="goPage"
        :page-size="2"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>


    <el-dialog title="岗位" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="编号" :label-width="formLabelWidth">
          <el-input v-model="form.postId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位编码" :label-width="formLabelWidth">
          <el-input v-model="form.postCode" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="岗位名称" :label-width="formLabelWidth">
          <el-input v-model="form.postName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="显示排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.postSort" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-input v-model="form.status" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建人" :label-width="formLabelWidth">
          <el-input v-model="form.createBy" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth">
          <el-date-picker
              v-model="form.createTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "PostView",
  data() {
    return {
      tableData:[],
      pageNum:1,
      total:0,
      formInline:{},
      form:{},
      formLabelWidth:'120px',
      dialogFormVisible:false,

      multipleSelection:[],
    }
  }, methods: {
    handleChange(value) {
      console.log(value);
    },
    excel(){
      location.href="http://localhost:8000//post/excel";
    },
    pishan(){
      let ids = this.multipleSelection.map(item=>item.postId);

        this.axios.post("http://localhost:8000/carsystem/sysPost/pishan?ids="+ids).then(res=>{
          if(res.data.code==200){
            this.$message.success(res.data.msg);
            this.list();
          }else{
            this.$message.error(res.data.msg);
          }
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    del(postId){
       this.axios.post("http://localhost:8000/carsystem/sysPost/del?postId="+postId).then(res=>{
           if(res.data.code==200){
              this.$message.success(res.data.msg);
              this.list();
           }else{
             this.$message.error(res.data.msg);
           }
       })
    },
    openEdit(row){
       this.form=row;
       this.dialogFormVisible=true;
    },
    add(){
      let postId = this.form.postId;
      if(!postId){
        this.save();
      }else{
        this.update();
      }
    },
    update(){
      this.axios.post("http://localhost:8000/carsystem/sysPost/update",this.form).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.msg);
          alert("修改成功")
          this.dialogFormVisible=false;
          location.href="/post";
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    save(){
        this.axios.post("http://localhost:8000/carsystem/sysPost/add",this.form).then(res=>{
          if(res.data.code==200){
            this.$message.success(res.data.msg);
            this.dialogFormVisible=false;
            alert("添加成功")
            location.href="/post";
          }else{
            this.$message.error(res.data.msg);
          }
        })
    },
    openadd(){
       this.form={};
       this.dialogFormVisible=true;
    },
    chong(){
      location.href="post";
    },
    goPage(page){
      this.pageNum=page;
      this.list();
    },
    list(){
      this.axios.post("http://localhost:8000/carsystem/sysPost/list?pageNum="+this.pageNum,this.formInline).then(res=>{
         this.tableData=res.data.data.list;
         this.total=res.data.data.total;
      })
    }
  }, created() {
     this.list();
  }
}
</script>

<style scoped>

</style>